﻿$(document).ready(() -> 


  $("#uploadEntry").click(() =>
      $.ajax({
         url : $("#entryUri").val()
         type : "POST"
         data : $("#uploadVal").val()
         datatype : "text"
         success : () => console.log "success"
      })
  )

  $("#putEntry").click(() =>
    rootEntry = Cofe.Entry.fromUrl(Cofe.utils.formatPath("{cofe}"))
    json = new Object()
    json.name = $("#name").val()
    json.type = "file"
    json.tags = "newTag"
    $.ajax({ 
          url : rootEntry.links.self.url
          type : "PUT"
          data : json
          dataType: "json"
          success: ((data, ts, xhr) => 
              entry = Cofe.Entry.fromJson(data)
              console.log entry              
              $("#entryUri").val(entry.links.stream.url)

          )
          error : ((data, ts, xhr) =>
              console.log "failed"
              console.log data
          )
          contentType:"application/json; charset=utf-8"
    })
  )

  $("#delEntry").click(() =>
    entry = Cofe.Entry.fromUrl(Cofe.utils.formatPath("{cofe}/" + $("#name").val()))
    console.log entry
    $.ajax({ 
          url : entry.links.self.url
          type : "DELETE"        
          success: ((data, ts, xhr) => 
              console.log "success"              
          )
          error : ((data, ts, xhr) =>
              console.log "failed"
              console.log data
          )
          contentType:"application/json; charset=utf-8"
    })
  )

  
  $(".uploader :button").click(() -> 
     file = this.parentElement[0].files[0]
     formData = new FormData();
     formData.append('file', file)
     console.log formData
     $.ajax({
       url: Cofe.utils.formatPath("{cofe}") + "&rel=upload"
       type: 'POST'
       data : formData
       cache: false
       contentType : false
       processData : false 
       success : () => console.log "success"
       error : () => console.log "error"
     })
  )

  
  

) 